package com.yami.shop.common.storage;

import com.aliyun.oss.OSSClient;
import com.yami.shop.common.bean.AliOss;

import java.io.ByteArrayInputStream;

/**
 * @author mengcy 2020-06-20
 */
public class AliOssStorage implements IStorage{

    private AliOss aliOss;

    private OSSClient client;


    public AliOssStorage(AliOss aliOss){
        this.aliOss = aliOss;
        client = new OSSClient(aliOss.getEndpoint(), aliOss.getAccessKey(), aliOss.getAccessSecret());
    }

    @Override
    public String getPrefixUrl() {
        return aliOss.getDomainUrl();
    }

    @Override
    public String upload(String fileName, byte[] bytes) throws Exception {
        client.putObject(aliOss.getBucket(), fileName, new ByteArrayInputStream(bytes));
        return fileName;
    }

    @Override
    public void delete(String fileName) throws Exception {
        client.deleteObject(aliOss.getBucket(), fileName);
    }
}
